package com.cfx.exercise_42;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.HeaderFooter;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.ColumnText;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;

public class RegPDF {

	Document doc = new Document();
	PdfWriter pw;
	private ByteArrayOutputStream ba;
	private PdfWriter pdfWriter;
	private static Font titleFont;// 设置字体大小
	private static Font textFont;// 设置字体大小
	private static Font keyFont;
	static {
		BaseFont bfChinese;
		try {

			bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED, true);
			// headfont = new Font(bfChinese, 10, Font.BOLD);// 设置字体大小
			titleFont = new Font(bfChinese, 18, Font.BOLD);// 设置字体大小
			textFont = new Font(bfChinese, 8, Font.NORMAL);// 设置字体大小
			keyFont = new Font(bfChinese, 10, Font.NORMAL);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public RegPDF(File file) {
		doc.setPageSize(PageSize.A4);// 设置页面大小
		try {
			pdfWriter = PdfWriter.getInstance(doc, new FileOutputStream(file));
			doc.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public ByteArrayOutputStream getBa() {
		return ba;
	}

	public void setBa(ByteArrayOutputStream ba) {
		this.ba = ba;
	}

	public RegPDF() {
		try {
			HeaderFooter footer = new HeaderFooter(new Phrase("第 ", keyFont), new Phrase(" 页", keyFont));
			footer.setAlignment(Element.ALIGN_CENTER);
			pw = PdfWriter.getInstance(doc, new FileOutputStream("pdf/receipt.pdf"));
			footer.setBorder(Rectangle.NO_BORDER);
			// doc.setFooter(footer);

		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
		} catch (DocumentException e1) {
			e1.printStackTrace();
		}
		ba = new ByteArrayOutputStream();
		doc.setPageSize(PageSize.A4);// 设置页面大小
		doc.setMargins(50, 50, 50, 50);
		try {
			pdfWriter = PdfWriter.getInstance(doc, ba);
			doc.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void onEndPage(PdfWriter writer, Document document) {

		Rectangle rect = writer.getBoxSize("art");
		switch (writer.getPageNumber() % 2) {
		case 0:
			ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_RIGHT, new Phrase("even header"),
					rect.getBorderWidthRight(), rect.getBorderWidthTop(), 0);
			break;
		case 1:
			ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_CENTER,
					new Phrase(String.format("%d", writer.getPageNumber())), 300f, 62f, 0);
			break;
		}
	}
	/**
	 * 返回pdf格式的公章刻制本案登记表
	 * 
	 * @param companyName_ch 中文单位名称
	 * @param companyName_en 英文名称
	 * @param address 单位地址
	 * @param creditCode 统一社会信用代码
	 * @param legalPerson 法人
	 * @param legalPersonId 法人身份证
	 * @param responPerson 经办人
	 * @param responPersonId 经办人身份证
	 * @param companyPhone 单位对外联系电话
	 * @param responPersonTel 经办人联系电话
	 * @param sealNumber 刻制印章数量
	 * @param sealName 刻制印章全称
	 * @throws Exception 
	 */
	public void getPDF(String companyName_ch,String companyName_en,String address,String creditCode,
						String legalPerson,String legalPersonId,String responPerson,String responPersonId,String companyPhone,
						String responPersonTel,int sealNumber,String sealName) throws Exception{
		Date date = new Date();
		Paragraph title = new Paragraph("公 章 刻 制 备 案 登 记 表",titleFont);
		title.setAlignment(Element.ALIGN_CENTER);
		Paragraph dateText = new Paragraph("申请日期：          "+date.getYear()+"       月"+date.getMonth()+"      日"+date.getDay(),keyFont);
		dateText.setSpacingBefore(30f);
		dateText.setSpacingAfter(5f);
		//外框
		PdfPTable table = new PdfPTable(1);
		PdfPCell cell = new PdfPCell();
		cell.setBorder(0);
		table.setWidthPercentage(100f);
		table.setSplitLate(false);// 自动换页
		
		//表格
		PdfPTable table1 = new  PdfPTable(5);
		table1.setWidthPercentage(100f);
		table1.setWidths(new float[] {20f,10f,10f,20f,40f});
		PdfPCell t1c1 = new PdfPCell(new Paragraph("单 位 名 称",keyFont));
		t1c1.setRowspan(2);
		t1c1.setMinimumHeight(40f);
		t1c1.setHorizontalAlignment(Element.ALIGN_CENTER);
		t1c1.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
		PdfPCell t1c2 = new PdfPCell(new Paragraph("中文",keyFont));
		t1c2.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
		PdfPCell t1c3 = new PdfPCell(new Paragraph(companyName_ch,textFont));
		t1c3.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
		t1c3.setColspan(3);
		PdfPCell t1c4 = new PdfPCell(new Paragraph("英文",keyFont));
		t1c4.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
		PdfPCell t1c5 = new PdfPCell(new Paragraph(companyName_en,textFont));
		t1c5.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
		t1c5.setColspan(3);
		PdfPCell t1c6 = new PdfPCell(new Paragraph("单位地址",keyFont));
	}
	//测试
	public static void main(String[] args) throws DocumentException, Exception {
		ReceiptPDF pdf = new ReceiptPDF();
		String zn = "北京科富兴科技有限公司";
		String en = "abc";
		String address = "北京市丰台区时代财富大厦1702";
		String number = "56481257845639215Y";
		String yy = "公司财务需要";
		String phone = "13756238469";
		String type = "国企";
		String comper = "张三丰";
		String id = "413264786541239875";
		String comperper = "张无忌";
		String id2 = "411424199004065953";
		String name = "丰台有名刻制社";
		String tel = "15823648596";
		String string = new String("财务专用章财务专用章财务专用章");
		List<String> list = new ArrayList<>();
		for(int i =0;i<10;i++){
			list.add(string);
		}
		pdf.getPDF(zn,en,address,number,yy,phone,type,comper,id,comperper,id2,list,name,tel);
	}
}
